package com.daykw.wkinfo.task;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.log4j.Logger;
import org.nutz.dao.entity.Record;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.daykw.wkinfo.http.HttpBase;
import com.daykw.wkinfo.utils.EhcacheUtils;
import com.daykw.wkinfo.utils.IPPoolMap;

public class ListIpPoolTask implements Runnable {
	private static Logger logger = Logger.getLogger(ListIpPoolTask.class);

	private static String url = "http://api.xdaili.cn/xdaili-api//privateProxy/applyStaticProxy?spiderId=17e3ff1e791d4334a4ff79abf49edde0&returnType=2&count=1";

	@Override
	public void run() {

		this.getIpPool();

	}

	private void getIpPool() {
		HttpBase http = new HttpBase();
		SimpleDateFormat ft = new SimpleDateFormat("yyyyMMddHHmmss");
		try {
			String html = http.get(url, null);
			JSONObject json = (JSONObject) JSONObject.parse(html);
			JSONArray list = json.getJSONArray("RESULT");
			for (int i = 0; i < list.size(); i++) {
				JSONObject j = list.getJSONObject(i);
				String ip = j.getString("ip");
				String port = j.getString("port");
				Record r = new Record();
				r.put("ips", ip + ":" + port);
				r.put("time", ft.format(new Date()));
				String ips = ip + ":" + port;
			
				if (!EhcacheUtils.getInstance().containsKey(ips)) {
					IPPoolMap.getLqueue().offer(r);
				}
			}
			logger.info("***********获取" + IPPoolMap.getLqueue().size() + "个IP*************");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

}
